
integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[0,0,0,0,0,0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer channel_dialog;
integer listen_id;
integer listen_id2;
string msg ="1";
list sideMenu=[" ","Back"];
string gPrefix;
string babel ="  ";
integer chatmethode=0;
string objektname;
string wearer;
integer on=0;
integer saying=0;
list textO=["no","yes"];
list textR=["woooof-woof","woooof!"];
integer random;
integer addstate=0;
integer gPage=0;
integer strictmode =0;
Debug(string _text)
{
    llSay(DEBUG_CHANNEL, _text);
}

string muffel(string mes)
{
    integer i;
    string sub = "";
    string rep = "";
    for (i = 0; i < llStringLength(mes); i++)
    {
        sub = llGetSubString(mes, i, i);
        rep = "";
        if (sub == "l")
        rep = "w";
        else  if (sub == "L")
        rep = "W";
        else  if (sub == "s")
        rep = "f";
        else  if (sub == "S")
        rep = "F";
        else  if (sub == "t")
        rep = "g";               
        else  if (sub == "T")
        rep = "G";
        
        if (rep != "")
        {
            mes = llDeleteSubString(mes, i, i);
            mes = llInsertString(mes, i, rep);
        }                 
    }
    return mes;
}

makePrefix()
{
    string tString =llKey2Name(llGetOwner());
    list my_list = llParseString2List(tString, [" "],["."]);
    string t1 =llList2String(my_list,0);
    t1=(llGetSubString(t1, 0, 0));
    string t2 =llList2String(my_list,1);
    t2=(llGetSubString(t2, 0, 0));
    gPrefix=t1+t2;
}



init()
{
    llRequestPermissions(llGetOwner(), PERMISSION_TRIGGER_ANIMATION);
    string tString =llKey2Name(llGetOwner());
    list my_list = llParseString2List(tString, [" "],["."]);
    string t1 =llList2String(my_list,0);
    objektname=llGetObjectName();
    wearer ="Puppy Buffy";
}

llRlvSay(string command)
{
    if(attachment==TRUE)
    {
        llOwnerSay("@"+command);
    }
    else
    {
        llSay(relaychannel,cmdname + "," + (string)victim  + "," + "@"+command);
    }
}

MakeRLVState()
{
    if(attachment==TRUE)
    {
        //onRez we make this to be secure if its an attachmentttt
        if(llList2Integer(statesMenu,0)==1)llRlvSay("redirchat:16=add");
    }
}

llLRP(integer element_new, integer placeinlist)
{
    statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

makeMenu()
{
    msg = "On || Off - Redirect On || Off\nReplace - Replaces listed words\nRep Strict - Replaces if found substring\nMuffel - Talk like gagged\nName - Set a new Name";
    gMenu=[];
    gMenu+=sideMenu;
    if(llList2Integer(statesMenu,0)==1)gMenu+="[x]On";
    else gMenu+="[ ]On";
    if(llList2Integer(statesMenu,1)==1)gMenu+="[x]Replace";
    else gMenu+="[ ]Replace";
    if(llList2Integer(statesMenu,2)==1)gMenu+="[x]Muffel";
    else gMenu+="[ ]Muffel";
    if(llList2Integer(statesMenu,3)==1)gMenu+="[x]Block";
    else gMenu+="[ ]Block";
    if(llList2Integer(statesMenu,4)==1)gMenu+="[x]Say";
    else gMenu+="[ ]Say";
    if(llList2Integer(statesMenu,5)==1)gMenu+="[x]Whisper";
    else gMenu+="[ ]Whisper";
    if(llList2Integer(statesMenu,6)==1)gMenu+="[x]Rep Strict";
    else gMenu+="[ ]Rep Strict";
    gMenu+=["Add Rep","Name","Rem Rep"];
    //Than display
    channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
    listen_id = llListen( channel_dialog, "", NULL_KEY, "");
    llDialog(ToucherID, msg,gMenu, channel_dialog);
    llSetTimerEvent(45.0);
}

makeTalk(string _message)
{
    string talking=_message;
    string _tt="";
    if(on==1)
    {

        if(chatmethode==1)//Rep
        {
        	if(strictmode==0)
        	{
	            talking = llToLower(_message);
	            list mes = llParseString2List(talking, [" "],["####"]);
	            integer _lenght = llGetListLength(mes);
	            integer _i=0;
	            for(_i;_i<_lenght;_i++)
	            {
	                string pp = llList2String( mes,_i);
	                if(~llListFindList(textO, (list)pp))
	                  {
	                      integer place = llListFindList(textO, [pp]);
	                      _tt=_tt+llList2String(textR,place)+" ";
	                }
	                else
	                { 
	                    _tt=_tt+pp+ " ";
	                }
	            }
        	}
        	else if(strictmode==1)
        	{
        		talking = llToLower(_message);
	            list mes = llParseString2List(talking, [" "],["####"]);
	            integer _lenght = llGetListLength(mes);
	            integer _i=0;
	            for(_i;_i<_lenght;_i++)
	            {
	            	integer _j=0;
	            	integer _lenght2 = llGetListLength(textO);
	            	string pp = llList2String( mes,_i);
	            	string pp3="";
	            	for(_j;_j<_lenght2;_j++)
	            	{
	            		string pp2 = llList2String( textO,_j);
						integer index = llSubStringIndex(pp,pp2);	   
						if(index != -1) 
	            		{
	            			pp3= llList2String( textR,_j);
	            			_tt=_tt+pp3+ " ";
	            		}         		
	            	}
	            	if(pp3=="")
	            	{
	            		pp3=pp;
	            		_tt=_tt+pp3+ " ";
	            	}
	            }
        	}
        }
        else if(chatmethode==2)//Muffel
        {
                _tt=muffel(talking);
        }
        else if(chatmethode==0)
        {
                _tt=talking;
        }
        else if(chatmethode==3)
        {
            _tt="";
        }
        
        if(saying ==1)
        {
            llSay(0,_tt);
        }
        else if(saying ==0)
        {
            llWhisper(0,_tt);
        }
    
    }
}

makeReplace()
{
    list _temp = textO;
    if(llGetListLength(_temp)<10)
    {
        _temp= [" ","[Back]"," "]+_temp;
    }
    else
    {

        _temp= pageMenu(_temp,gPage);
    }
    msg = "Remove Item By Click at";
    channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
    listen_id = llListen( channel_dialog, "", NULL_KEY, "");
    llDialog(ToucherID, msg,_temp, channel_dialog);
    llSetTimerEvent(45.0);
}

list pageMenu(list in ,integer page)
{
    list out;
    integer maxpages =llGetListLength(in)/9;
    page = (page)%(maxpages+1);
    out =["<<","[Back]",">>"];
    integer _listcount =(page *9)+9;
    integer _i=_listcount-9;
    
    for (_i;_i<_listcount;_i++)
    {
        if(_i<=llGetListLength(in)&&llList2String(in,_i)!="")
        out+=llList2String(in,_i);
    }
    return out;
}

default {
    state_entry() 
    {
        makePrefix();
        init();    
    }
    
    listen(integer channel, string name, key id, string message)
    {
        
        if(channel==random)
        {
            llListenRemove(random);
            if(addstate==0)
            {
                list mes = llParseString2List(message, [","],["."]);
                string p1=llToLower(llList2String(mes,0));
                string p2=llToLower(llList2String(mes,1));
                if(p2!="")
                {
                    textO+=p1;
                    textR+=p2;
                }
            }
            else if(addstate==1)
            {
                wearer=message;
            }
        }
        if(channel==channel_dialog)
        {
            llListenRemove(listen_id);
            llSetTimerEvent(0.0);
            if(message=="Rem Rep")
            {
                makeReplace();
            }
            else if(~llListFindList(textO, (list)message))
              {
                  llOwnerSay(message);
                  integer place = llListFindList(textO, [message]);
                  textO=llDeleteSubList(textO, place, place);
                  textR=llDeleteSubList(textR, place, place);
                  makeReplace();
              }
            else if(message=="<<")
              {
                  gPage--;
                integer maxpages =llGetListLength(textO)/9;
                if(gPage<0)
                {
                    gPage = maxpages;
                }
                makeReplace();
              }
              else if(message=="[Back]")
              {
                  makeMenu();
              }
              else if(message==">>")
              {
                  gPage++;
                  makeReplace();
              }
            else if(message=="[x]On")
            {
                llLRP(0,0);
                llRlvSay("redirchat:16=rem");
                llListenRemove(listen_id2);
                on=1;
                makeMenu();
            }
            else if(message=="[ ]On")
            {
                llLRP(1,0);
                llRlvSay("redirchat:16=add");
                listen_id2=llListen( 16, "", llGetOwner(), "");
                on=1;
                makeMenu();
            }
            else if(message=="[x]Replace")
            {
                llLRP(0,1);
                makeMenu();
                chatmethode=0;
            }
            else if(message=="[ ]Replace")
            {
                llLRP(1,1);
                llLRP(0,2);
                llLRP(0,3);    
                chatmethode=1;
                makeMenu();
            } 
            //
            else if(message=="[x]Rep Strict")
            {
                llLRP(0,6);
                makeMenu();
                strictmode=0;
                chatmethode=0;
            }
            else if(message=="[ ]Rep Strict")
            {
                llLRP(1,6);
                chatmethode=1;
                strictmode=1;
                makeMenu();
            } 
            else if(message=="[x]Muffel")
            {
                llLRP(0,2);
                chatmethode=0;
                makeMenu();
            }
            else if(message=="[ ]Muffel")
            {
                llLRP(1,2);
                llLRP(0,1);
                llLRP(0,3);    
                chatmethode=2;
                makeMenu();
                
            } 
            else if(message=="[x]Block")
            {
                llLRP(0,3);
                chatmethode=0;
                makeMenu();
            }
            else if(message=="[ ]Block")
            {
                llLRP(1,3);
                llLRP(0,2);
                llLRP(0,1);    
                chatmethode=3;
                makeMenu();
            }
            else if(message=="[x]Say")
            {
                llLRP(0,4);
                saying=1;
                makeMenu();
            }
            else if(message=="[ ]Say")
            {
                llLRP(1,4);
                llLRP(0,5);
                saying=1;
                makeMenu();
            } 
            else if(message=="[x]Whisper")
            {
                llLRP(0,5);
                saying=1;
                makeMenu();
            }
            else if(message=="[ ]Whisper")
            {
                llLRP(1,5);
                llLRP(0,4);
                saying=0;
                makeMenu();
            }       
            else if(message=="Add Rep")
            {
                float grand=llFrand(13333337);
                random= llRound(grand);
                listen_id = llListen( random, "", ToucherID, "");
                addstate=0;
                llInstantMessage(ToucherID, "Add Replace words on: "+(string)random +" /<channel> <word>,<word group>" );
            } 
            else if(message=="Name")
            {
                float grand=llFrand(13333337);
                random= llRound(grand);
                listen_id = llListen( random, "", ToucherID, "");
                addstate=1;
                llInstantMessage(ToucherID, "Change name on: "+(string)random );
            } 
            else if(message=="Back")llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_MISC", ToucherID);
        }
        else if(channel==16)
        {
                llSetObjectName(wearer);
                makeTalk(message);
                llSetObjectName(objektname);
        }
    }
    
    on_rez(integer on)
    {
    
    }
    
    link_message(integer sender, integer num, string str, key id)
      {
          
          if(num ==gMenuLink)
          {
              if(str==llGetScriptName())
              {
                  llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
                  ToucherID=id;
                  makeMenu();
              }
          }
          if(num ==gDemon)
          {
              if(str =="attachment")attachment=TRUE;
              else if(str =="device")attachment=FALSE;
              else if(str!=llGetScriptName())
              {
                llListenRemove(listen_id);
                llListenRemove(random);

              }
          }
          if(num==gPlugin)
          {
              if(str=="ON_REZ")
              {
                  MakeRLVState();
              }
              if(str=="RESET")
              {
                  statesMenu=[0,0,0,0];
              }
          }
          if(num==gVic)
          {
                  victim=(key)str;
                  cmdname = (string)llGetKey();
          }
      }
      
      timer()
    {
        llSetTimerEvent(0.0);
        llListenRemove(listen_id);
        llListenRemove(random);
    }
}
